package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.LeaveApplyMapper;
import com.wjh.pojo.Expressage;
import com.wjh.pojo.LeaveApply;
import com.wjh.service.LeaveApplyService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (LeaveApply)表服务实现类
 *
 * @author makejava
 * @since 2024-03-21 20:17:55
 */
@Service("leaveApplyService")
public class LeaveApplyServiceImpl extends ServiceImpl<LeaveApplyMapper, LeaveApply> implements LeaveApplyService {

    @Override
    public ResponseResult queryLeaveList(Integer pageNum, Integer pageSize, Integer isRatify, Integer leaveType,
                                         String name, Integer ratifyType,Integer state) {
        LambdaQueryWrapper<LeaveApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,LeaveApply::getCourierName,name);
        queryWrapper.eq(LeaveApply::getIsRatify,isRatify);
        queryWrapper.eq(leaveType != null,LeaveApply::getLeaveType,leaveType);
        queryWrapper.eq(ratifyType != null,LeaveApply::getRatifyType,ratifyType);
        queryWrapper.eq(state != null,LeaveApply::getState,state);
        //分页查询所有
        Page<LeaveApply> leaveApplyPage = new Page<>(pageNum,pageSize);
        Page<LeaveApply> page = page(leaveApplyPage, queryWrapper);
        List<LeaveApply> records = page.getRecords();
        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult ratifyLeave(LeaveApply leaveApply) {
        //是否同意状态为空，通过客户端不会出现该问题
        if(leaveApply.getRatifyType() == null){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        //数据不存在错误走的逻辑，如果不为0或1证明不是客户端发出的请求
        if(leaveApply.getRatifyType() == 0 ||  leaveApply.getRatifyType() == 1){
            updateById(leaveApply);
        } else {
            throw new SystemException(AppHttpCodeEnum.LEAVE_RATIFY_STATE_NOTNULL);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delLeaveByIds(List<Integer> id) {
        removeByIds(id);
        return ResponseResult.okResult();
    }
}
